/*
 * Copyright (c) 2019 DawnFramework. All Rights Reserved.
 * Licensed under the MIT (LICENSE) license.
 */

package org.dawn.common.core.utils;

import org.dawn.common.core.mode.Dict;

/**
 * 字典工具
 * @author Willard Hu on 2017/4/25.
 */
public class Dicts {

    /**
     * 通过枚举code查询枚举类
     */
    public static <T extends Dict> T find(String key, Class<T> dictImplClz) {
        if (dictImplClz == null || !dictImplClz.isEnum() || Strings.isBlank(key)) {
            return null;
        }
        for (T t : dictImplClz.getEnumConstants()) {
            if (Strings.equals(t.key(), key)) {
                return t;
            }
        }
        return null;
    }

    /**
     * 判断code是否在指定枚举值中
     */
    public static <T extends Dict> boolean contains(String key, Class<T> dictImplClz) {
        if (dictImplClz == null || !dictImplClz.isEnum() || Strings.isBlank(key)) {
            return false;
        }
        for (T t : dictImplClz.getEnumConstants()) {
            if (Strings.equals(t.key(), key)) {
                return true;
            }
        }
        return false;
    }
}
